package code.main.controller.table
{
	import code.common.assets.AssetsClassName;
	import code.main.view.table.PlayBtn;
	
	import com.greensock.TweenLite;
	import com.yy.manager.ResourceManager;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	import flash.utils.Timer;

	/**
	 *下注控制器 
	 * @author YY
	 * 
	 */
	public class AddBetCtrl
	{
		private var _direction:int = 10;
		private var _increaseRaiseBtn:DisplayObject = null;
		private var _sliderLine:MovieClip = null;
		private var _sliderTrack:MovieClip = null;
		private var _raiseBtn:DisplayObject = null;
		private var _timer:Timer = null;
		private var _maxRaise:int = 0;
		private var _chipsCount:TextField = null;
		private var _sliTrClkA:Sprite;
		private var _addAll:SimpleButton = null;
		private var _sliderBar:MovieClip = null;
		private var _clipView:MovieClip = null;
		private var _reduceRaiseBtn:DisplayObject = null;
		private var _value:int = 0;
		private var _needCall:int = 0;
		private var _cb:Function;//下注回调
		public function AddBetCtrl(cb:Function, view:Object){
			_clipView = null;
			_increaseRaiseBtn = null;
			_reduceRaiseBtn = null;
			_sliderBar = null;
			_sliderTrack = null;
			_sliderLine = null;
			_addAll = null;
			_raiseBtn = null;
			_chipsCount = null;
			_maxRaise = 0;
			_needCall = 0;
			_value = 0;
			_direction = 1;
			_timer = null;
			super();
			_cb = cb;
			_clipView = MovieClip(view);
			_increaseRaiseBtn = _clipView.increaseRaiseBtn;
			_reduceRaiseBtn = _clipView.reduceRaiseBtn;
			_addAll = _clipView.addAll;
			_sliderTrack = _clipView.sliderTrack;
			_sliderLine = _clipView.sliderLine;
			_sliderLine.mouseEnabled = false;
			_sliderTrack.x = int(_sliderTrack.x);
			_sliderTrack.y = int(_sliderTrack.y);
			_sliderTrack.width = int(_sliderTrack.width);
			_sliderBar = _clipView.sliderBar;
			_sliderBar.buttonMode = true;
			_sliderBar.y = int(_sliderBar.y);
			_sliderBar.width = int(_sliderBar.width);
			_sliderBar.gotoAndStop("out");
			
			_raiseBtn = _clipView.addChild(new PlayBtn(_clipView.raiseBtn));
			_chipsCount = _clipView.chipsCount;
			_chipsCount.restrict = "0-9";
			_sliTrClkA = new Sprite();
			_sliTrClkA.graphics.beginFill(0xFFFFFF);
			_sliTrClkA.graphics.drawRect(0, ((-(_sliderTrack.height) * 7) / 2), _sliderTrack.width, (_sliderTrack.height * 8));
			_sliTrClkA.graphics.endFill();
			_sliTrClkA.alpha = 0;
			_sliderTrack.addChild(_sliTrClkA);
			_sliderTrack.buttonMode = true;
			_timer = new Timer(500);
			hideRaiseCall();
		}
		private function addEvent():void{
			_sliderBar.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			_sliderBar.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			_sliderBar.addEventListener(MouseEvent.ROLL_OUT, outHandler);
			_sliderTrack.addEventListener(MouseEvent.CLICK, changeSliderBar);
			_increaseRaiseBtn.addEventListener(MouseEvent.MOUSE_DOWN, startRaiseHandler);
			_reduceRaiseBtn.addEventListener(MouseEvent.MOUSE_DOWN, startReduceHandler);
			_chipsCount.addEventListener(KeyboardEvent.KEY_DOWN, inputHandler);
			_raiseBtn.addEventListener(MouseEvent.CLICK, clickHandler);
			((_chipsCount.stage) && (_chipsCount.stage.addEventListener(Event.ACTIVATE, onActivate)));
			_chipsCount.addEventListener(Event.CHANGE, onTextInput);
//			_addAll.addEventListener(MouseEvent.CLICK, addAddHandler);
		}
		private function callHandler(_arg1:Event=null):void{
			_value = (_value + _direction);
			setValue();
		}
		private function stopDragHandler(_arg1:MouseEvent):void{
			_arg1.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			_sliderBar.removeEventListener(Event.ENTER_FRAME, dragHandler);
			_sliderBar.stopDrag();
		}
		private function inputHandler(_arg1:KeyboardEvent):void{
			if (_arg1.keyCode == Keyboard.ENTER){
				clickHandler(null);
			};
		}
		private function outHandler(_arg1:MouseEvent):void{
			_sliderBar.gotoAndStop("out");
		}
		public function setMaxRaist(_arg1:int):void{
			_maxRaise = (_maxRaise - _arg1);
			setValue();
		}
		private function clickHandler(_arg1:MouseEvent):void{
			if (!isUseValue(_value)){
				setValue();
				return;
			};
			setValue();
			_cb(_value);
			//            RoomManager.instance.room.roomOperation.hideChipin();
			//            SocketManager.instance.RequestChipin(ChipInManager.CHIPIN_RAISE, _value, SelfData.instance.userkey);
			//            LittleTips.instance.getState(9);
		}
		private function mouseUphandler(_arg1:MouseEvent):void{
			_arg1.currentTarget.removeEventListener(_arg1.type, arguments.callee);
			_timer.stop();
			_timer.reset();
			_timer.delay = 500;
		}
		private function autoEnterFous():void{
			if (((_chipsCount.stage) && (_chipsCount.visible))){
				_chipsCount.setSelection(_chipsCount.length, _chipsCount.length);
				_chipsCount.stage.focus = _chipsCount;
			};
		}
		private function adjustValue(_arg1:int, _arg2:Boolean=true):int{
			_chipsCount.type = TextFieldType.INPUT;
			if (_maxRaise == _needCall){
				_chipsCount.type = TextFieldType.DYNAMIC;
				_arg1 = _maxRaise;
			};
			if (_arg1 > _maxRaise){
				_arg1 = _maxRaise;
			};
			if (_arg2){
				if (_arg1 < _needCall){
					_arg1 = _needCall;
				};
			};
			return (_arg1);
		}
		private function startDragHandler(_arg1:MouseEvent):void{
			_sliderBar.gotoAndStop("down");
			_sliderBar.startDrag(false, new Rectangle((_sliderTrack.x + (_sliderBar.width / 2)), _sliderBar.y, (_sliderTrack.width - _sliderBar.width), 0));
			_sliderBar.addEventListener(Event.ENTER_FRAME, dragHandler);
			_sliderBar.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}
		private function setValue(_arg1:Boolean=true):void{
			var _local2:Number;
			_value = adjustValue(_value, _arg1);
			_chipsCount.text = _value.toString();
			if (_maxRaise != _needCall){
				_local2 = ((_sliderTrack.x + (((_value - _needCall) * (_sliderTrack.width - _sliderBar.width)) / (_maxRaise - _needCall))) + (_sliderBar.width / 2));
			} else {
				_local2 = (_sliderTrack.x + (_sliderBar.width / 2));
			};
			TweenLite.to(_sliderBar, 0.2, {x:_local2});
			TweenLite.to(_sliderLine, 0.2, {scaleX:(((_local2 - _sliderTrack.x) - (_sliderBar.width / 2)) / (_sliderTrack.width - _sliderBar.width))});
		}
		private function stopCallHandler(_arg1:MouseEvent):void{
			_arg1.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopCallHandler);
			_sliderBar.removeEventListener(Event.ENTER_FRAME, callHandler);
		}
		private function changeSliderBar(_arg1:Event):void{
			var _local2:Number;
			if (_clipView.mouseX > ((_sliderTrack.x + _sliderTrack.width) - (_sliderBar.width / 2))){
				_local2 = ((_sliderTrack.x + _sliderTrack.width) - (_sliderBar.width / 2));
			} else {
				if (_clipView.mouseX < (_sliderTrack.x + (_sliderBar.width / 2))){
					_local2 = (_sliderTrack.x + (_sliderBar.width / 2));
				} else {
					_local2 = _clipView.mouseX;
				};
			};
			TweenLite.to(_sliderBar, 0.2, {x:_local2});
			TweenLite.to(_sliderLine, 0.2, {scaleX:(((_local2 - _sliderTrack.x) - (_sliderBar.width / 2)) / (_sliderTrack.width - _sliderBar.width))});
			_value = ((((_maxRaise - _needCall) * ((_local2 - _sliderTrack.x) - (_sliderBar.width / 2))) / (_sliderTrack.width - _sliderBar.width)) + _needCall);
			_value = adjustValue(_value);
			_chipsCount.text = _value.toString();
		}
		private function getValue(_arg1:String):int{
			_arg1 = _arg1.replace(/\$|,|\s*/g, "");
			return (int(_arg1));
		}
		private function startRaiseHandler(_arg1:MouseEvent):void{
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
			_timer.start();
			if (_clipView.stage){
				_clipView.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUphandler);
			};
			if ((_direction < 0)){
				_direction = (_direction * -1);
			} else {
				_direction;
			};
			callHandler();
		}
		private function startReduceHandler(_arg1:MouseEvent):void{
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
			_timer.start();
			if (_clipView.stage){
				_clipView.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUphandler);
			};
			if ((_direction < 0)){
				_direction;
			} else {
				_direction = (_direction * -1);
			};
			callHandler();
		}
		private function timerHandler(_arg1:TimerEvent):void{
			if (_timer.delay != 50){
				_timer.delay = 50;
			};
			callHandler();
		}
		public function hideRaiseCall():void{
			_clipView.visible = false;
			removeEvent();
		}
		private function overHandler(_arg1:MouseEvent):void{
			_sliderBar.gotoAndStop("over");
		}
		public function set value(_arg1:int):void{
			_value = _arg1;
			setValue();
		}
		private function addAddHandler(_arg1:MouseEvent):void{
			_value = _maxRaise;
			setValue(false);
		}
		private function onActivate(_arg1:Event):void{
		}
		private function removeEvent():void{
			_sliderBar.removeEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			_sliderBar.removeEventListener(MouseEvent.ROLL_OVER, overHandler);
			_sliderBar.removeEventListener(MouseEvent.ROLL_OUT, outHandler);
			_sliderTrack.removeEventListener(MouseEvent.CLICK, changeSliderBar);
			_increaseRaiseBtn.removeEventListener(MouseEvent.MOUSE_DOWN, startRaiseHandler);
			_reduceRaiseBtn.removeEventListener(MouseEvent.MOUSE_DOWN, startReduceHandler);
			_chipsCount.removeEventListener(KeyboardEvent.KEY_DOWN, inputHandler);
			_raiseBtn.removeEventListener(MouseEvent.CLICK, clickHandler);
			_chipsCount.removeEventListener(Event.CHANGE, onTextInput);
			((_chipsCount.stage) && (_chipsCount.stage.removeEventListener(Event.ACTIVATE, onActivate)));
//			_addAll.removeEventListener(MouseEvent.CLICK, addAddHandler);
		}
		public function get value():int{
			return (_value);
		}
		private function isUseValue(_arg1:int):Boolean{
			if (_arg1 > _maxRaise){
				return (false);
			};
			if (_arg1 < _needCall){
				return (false);
			};
			return (true);
		}
		public function showRaiseCall(_arg1:int, _arg2:int, _arg3:int):void{
			_needCall = _arg1;
			_maxRaise = _arg2;
			if (_arg1 == _arg2){
				_needCall = _maxRaise;
			};
			_direction = _arg3;
			value = _arg1;
			_clipView.visible = true;
			addEvent();
		}
		private function dragHandler(_arg1:Event):void{
			_sliderLine.scaleX = (((_sliderBar.x - _sliderTrack.x) - (_sliderBar.width / 2)) / (_sliderTrack.width - _sliderBar.width));
			_value = ((((_maxRaise - _needCall) * ((_sliderBar.x - _sliderTrack.x) - (_sliderBar.width / 2))) / (_sliderTrack.width - _sliderBar.width)) + _needCall);
			_value = adjustValue(_value);
			_chipsCount.text = _value.toString();
		}
		private function onTextInput(_arg1:Event):void{
			_value = getValue(_chipsCount.text);
			setValue(false);
			_chipsCount.setSelection(_chipsCount.length, _chipsCount.length);
		}
		
	}
}